package com.wsd.serviceImpl;

import com.wsd.common.LoanCalculation;
import com.wsd.constant.RepaymentPlanStatusEnum;
import com.wsd.constant.RepaymentTypeEnum;
import com.wsd.common.TypeSwitch;
import com.wsd.dao.LoanInformationMapper;
import com.wsd.dao.LoanLimitMapper;
import com.wsd.dao.RepaymentPlanMapper;
import com.wsd.domain.*;
import com.wsd.service.ILoanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.logging.Logger;

/**
 * @author Wan Yucan
 * @date 2020/7/15-14:13
 */
@Service
public class LoanServiceImpl implements ILoanService {
    @Autowired
    private LoanCalculation loanCalculation;
    @Autowired
    private LoanInformation loanInformation;
    @Autowired
    private LoanInformationMapper loanInformationMapper;
    @Autowired
    private RepaymentPlanMapper repaymentPlanMapper;
    @Autowired
    private TypeSwitch typeSwitch;
    @Autowired
    private ILoanService loanService;
    @Autowired
    private LoanLimitMapper loanLimitMapper;
    @Override
    public List<RepayPlan> loanRepayment(Loan loan){
        //枚举类获取贷款类型
        RepaymentTypeEnum repaymentTypeEnum=RepaymentTypeEnum.getType(loan.getRepaymentType());
        switch (repaymentTypeEnum){
            //等额本金还款
            case EQUAL_PRINCIPAL:
                return loanCalculation.equalPrincipal(loan);
            //等额本息还款
            case EQUAL_PRINCIPAL_AND_INTEREST:
                return loanCalculation.equalPrincipalAndInterest(loan);
            //先息后本还款
            case INTEREST_BEFORE_PRINCIPAL:
                return loanCalculation.interesrBeforePrincipal(loan);
            default:
                Logger.getLogger("log/warn.log");
                return null;
        }
    }
    @Override
    public boolean ensureLoan(Loan loan){
        LoanLimit loanLimit=loanLimitMapper.selectByCustomerId(loan.getUserAccount());
        //判断用户额度是否满足
        if(loanLimit!=null&&loan.getLoanAmount().compareTo(loanLimit.getAvailableLimit())!=1){
            //更新贷款额度
            loanLimit.setAvailableLimit(loanLimit.getAvailableLimit().subtract(loan.getLoanAmount()));
            loanLimit.setUsedLimit(loanLimit.getUsedLimit().add(loan.getLoanAmount()));
            loanLimitMapper.updateByPrimaryKeySelective(loanLimit);
            //插入贷款信息
            loanInformation=typeSwitch.loanToLoanInformation(loan);
            loanInformationMapper.insert(loanInformation);
            //插入还款计划
            List<RepayPlan> repayPlans=loanService.loanRepayment(loan);
            repayPlans.forEach(repayPlan -> repayPlan.setLoanId(loanInformation.getLoanId()));
            repaymentPlanMapper.insertRepaymentPlans(repayPlans);
            return  true;
        }else {
            return false;
        }
    }
    @Override
    public  List<LoanInformation> applyingLoanInformation(){
        return loanInformationMapper.findLoanListByStatus(RepaymentPlanStatusEnum.NOT_REPAYMENT.getType());
    }
    @Override
    public void updateLoanInformationStatus(List<LoanInformation> loanInformations) {
        if (loanInformations.size()!=0) {
            loanInformationMapper.updateLoanStatus(loanInformations);
        }
        return;
    }
}